#!/bin/sh
#v2.14 Puppy now has XDG menus.
#this script builds the menus from template files.
#Any templates can be placed into /etc/xdg/templates, and the file must be
#named to show its final destination. For example, the template for JWM:
# _root_.jwmrc
#...the '_' will be converted to a '/', so the generated JWM config file is:
# /root/.jwmrc
# 5jan2008: fbpanel,lxpanel support developed by plinej.
#100404 BK added 'variconlinks' for my fbpanel pkg.
#100427 when called via /etc/rc.d/rc.update, HOME is '/' (needed by some of the menu generating apps).
#120207 translation of some SSS strings. refer /usr/share/sss/menu_strings/
#120208 fix in case this script gets called with LANG=C
#120209 this script now called from /usr/sbin/quicksetup, whenever locale is changed.
#120216 sss translation file now uses simple sed expressions.
#120524 bug fix.

#100427
[ ! "$HOME" ] && HOME='/root'
[ "$HOME" = "/" ] && HOME='/root'
export HOME

[ -f $HOME/.jwm/menuheights ] && . $HOME/.jwm/menuheights

# remove csh shell files #precaution
CSH=$(find /etc/profile.d -name *.csh)
[ "$CSH" ] && rm /etc/profile.d/*.csh

if [ "$LANG" = "C" ];then #120208
 LANG="`grep '^LANG=' /etc/profile | cut -f 2 -d '=' | cut -f 1 -d ' '`"
 export LANG
fi
LANG1="`echo -n $LANG | cut -f 1 -d '_'`"  #120207 ex: de

TEMPLATES="`ls -1 /etc/xdg/templates | tr '\n' ' '`"

for ONETPL in $TEMPLATES #ex: _root_.jwmrc
do
 [ "$ONETPL" = "README.txt" ] && continue
 ONEDEST="`echo -n "$ONETPL" | sed -e 's/_/\//g'`"
 ONESRC="/etc/xdg/templates/$ONETPL"
 echo "Generating $ONEDEST..."
 
 [ -f $ONEDEST ] && mv -f $ONEDEST ${ONEDEST}-previous

 cat $ONESRC |
 while read ONELINE
 do
  EXECMENU="`echo -n "$ONELINE" | grep -o 'PUPPYMENU.*' | cut -f 2-5 -d ' '`"
  if echo "$ONELINE" | grep -q "MENHEIGHT" ;then #131213 designed to be backward compatible
   [ "$MENHEIGHT" ] && echo $ONELINE | sed "s%MENHEIGHT%$MENHEIGHT%" >> $ONEDEST \
   || echo $ONELINE | sed "s%MENHEIGHT%24%" >> $ONEDEST
  elif [ "$EXECMENU" = "" ];then
   echo "$ONELINE" >> $ONEDEST
  else
   ${EXECMENU} ${MENHEIGHT} >> ${ONEDEST}
  fi
 done
 
 #120207 translate some strings... 120216...
 if [ "$LANG1" != "en" ];then
  if [ -f /usr/share/sss/menu_strings/menu_strings.${LANG1} ];then
   sPTN="/^\[${ONETPL}\]/,/^$/p" #this is a multi-line block find expression.
   CODEBLOCK="`sed -n "$sPTN" /usr/share/sss/menu_strings/menu_strings.${LANG1} | sed -e '/^#/d' -e '/%%/d' -e '/^$/d' -e '/^\[/d'`" #extracts just the relevant block of lines.
   if [ "$CODEBLOCK" ];then
    echo "$CODEBLOCK" > /tmp/fixmenus-translationblock
    #121124 ensure that all [ ] are escaped... 121125 revert... 121126 restore, plus escape '.' chars...
    sed -i -e 's%\[%\\[%g' -e 's$\]$\\]$g' -e 's%\\\\\[%\\[%g' -e 's%\\\\\]%\\]%g' /tmp/fixmenus-translationblock
    sed -i -e 's%\.%\\.%g' -e 's%\\\\\.%\\.%g' /tmp/fixmenus-translationblock #note: 2nd ptn gets rid of prior escape char, so there remains just one.
    sed -i -f /tmp/fixmenus-translationblock ${ONEDEST}
   fi
  fi
 fi

done

#w001 support for fbpanel, lxpanel, openbox, fluxbox, pekwm...
[ `which variconlinks` ] && variconlinks #100404 for my fbpanel pkg.
[ `which tempicons` ] && tempicons
[ `which fbpanel_menu_refresh` ] && fbpanel_menu_refresh
[ `which lxpanel_menu_refresh` ] && lxpanel_menu_refresh
[ `which jwm2fluxbox` ] && jwm2fluxbox  ##current fluxbox_menu_refresh doesn't support menu icons while this does
[ `which obmenu-refresh` ] && obmenu-refresh
[ `which jwm2pekwm` ] && jwm2pekwm

###END###
